//
//  UIButton+HKExtension.swift
//  App架构Swift
//
//  Created by 胡锦涛 on 2019/2/19.
//  Copyright © 2019 胡锦涛. All rights reserved.
//

import UIKit
// MARK: - UIButton控件扩展
extension UIButton {
    
    // convenience : 便利,使用convenience修饰的构造函数叫做便利构造函数
    // 遍历构造函数通常用在对系统的类进行构造函数的扩充时使用
    /*
     便利构造函数的特点
     1.遍历构造函数通常都是写在extension里面
     2.遍历构造函数init前面需要加载convenience
     3.在遍历构造函数中需要明确的调用self.init()
     */
    convenience init (imageName : String, bgImageName : String) {
        self.init()
        setImage(UIImage(named: imageName), for: .normal)
        setImage(UIImage(named: imageName + "_highlighted"), for: .highlighted)
        setBackgroundImage(UIImage(named: bgImageName), for: .normal)
        setBackgroundImage(UIImage(named: bgImageName + "_highlighted"), for: .highlighted)
        sizeToFit()
    }
    
    convenience init(bgColor : UIColor, fontSize : CGFloat, title : String) {
        self.init()
        
        setTitle(title, for: .normal)
        backgroundColor = bgColor
        titleLabel?.font = UIFont.systemFont(ofSize: fontSize)
    }
    
    convenience init(titleFont: UIFont = UIFont.systemFont(ofSize: 14), normalTitleColor: UIColor = UIColor.black, normalTitle: String = "", target: Any?, action: Selector, superView: UIView?) {
        self.init(type: .custom)
        self.titleLabel?.font = titleFont
        self.setTitleColor(normalTitleColor, for: .normal)
        self.setTitle(normalTitle, for: .normal)
        self.addTarget(target, action: action, for: .touchUpInside)
        if superView != nil {
            superView!.addSubview(self)
        }
    }
    
    convenience init(normalImage: UIImage, target: Any?, action: Selector, superView: UIView?) {
        self.init(type: .custom)
        self.setImage(normalImage, for: .normal)
        self.addTarget(target, action: action, for: .touchUpInside)
        if superView != nil {
            superView!.addSubview(self)
        }
    }
    
}
extension UIButton{
    
    ///布局调整(标题、位置、间距)
    func hk_AdjustPosition(title: String, position: UIView.ContentMode,
                                     spacing: CGFloat) {
        let imageSize = self.imageRect(forContentRect: self.frame)
        let titleFont = self.titleLabel?.font!
        let titleSize = title.size(withAttributes: [NSAttributedString.Key.font: titleFont!])
        
        var titleInsets: UIEdgeInsets
        var imageInsets: UIEdgeInsets
        
        switch (position){
        case .top:
            titleInsets = UIEdgeInsets(top: -(imageSize.height + titleSize.height + spacing),
                                       left: -(imageSize.width), bottom: 0, right: 0)
            imageInsets = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: -titleSize.width)
        case .bottom:
            titleInsets = UIEdgeInsets(top: (imageSize.height + titleSize.height + spacing),
                                       left: -(imageSize.width), bottom: 0, right: 0)
            imageInsets = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: -titleSize.width)
        case .left:
            titleInsets = UIEdgeInsets(top: 0, left: -(imageSize.width * 2), bottom: 0, right: 0)
            imageInsets = UIEdgeInsets(top: 0, left: 0, bottom: 0,
                                       right: -(titleSize.width * 2 + spacing))
        case .right:
            titleInsets = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: -spacing)
            imageInsets = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
        default:
            titleInsets = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
            imageInsets = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
        }
        
        self.titleEdgeInsets = titleInsets
        self.imageEdgeInsets = imageInsets
    }
}
extension UIButton {
    
    static var topNameKey : Character!
    static var leftNameKey : Character!
    static var bottomNameKey : Character!
    static var rightNameKey : Character!
    
    /// 添加按钮的 四周的触发范围
    func hk_enlargeTouchSize(top : CGFloat, left : CGFloat, bottom : CGFloat, right : CGFloat){
        objc_setAssociatedObject(self, &UIButton.topNameKey, NSNumber.init(value: Float(top)), objc_AssociationPolicy.OBJC_ASSOCIATION_COPY_NONATOMIC)
        objc_setAssociatedObject(self, &UIButton.leftNameKey, NSNumber.init(value: Float(left)), objc_AssociationPolicy.OBJC_ASSOCIATION_COPY_NONATOMIC)
        objc_setAssociatedObject(self, &UIButton.bottomNameKey, NSNumber.init(value: Float(bottom)), objc_AssociationPolicy.OBJC_ASSOCIATION_COPY_NONATOMIC)
        objc_setAssociatedObject(self, &UIButton.rightNameKey, NSNumber.init(value: Float(right)), objc_AssociationPolicy.OBJC_ASSOCIATION_COPY_NONATOMIC)
    }
    
    ///Button扩大点击区域
    func hk_expandRect() -> CGRect {
        let topEdege = NSNumber.init(value: objc_getAssociatedObject(self, &UIButton.topNameKey) as? Float ?? 0)
        let leftEdge = NSNumber.init(value: objc_getAssociatedObject(self, &UIButton.leftNameKey) as? Float ?? 0)
        let bottomEdge = NSNumber.init(value: objc_getAssociatedObject(self, &UIButton.bottomNameKey) as? Float ?? 0)
        let rightEdge = NSNumber.init(value: objc_getAssociatedObject(self, &UIButton.rightNameKey) as? Float ?? 0)
        
        if topEdege.floatValue != 0 || leftEdge.floatValue != 0 || bottomEdge.floatValue != 0 || rightEdge.floatValue != 0 {
            return CGRect.init(x: self.bounds.origin.x - CGFloat.init(truncating: leftEdge),
                               y: self.bounds.origin.y - CGFloat.init(truncating: topEdege),
                               width: self.bounds.size.width + CGFloat.init(truncating: leftEdge) + CGFloat.init(truncating: rightEdge),
                               height: self.bounds.size.height + CGFloat.init(truncating: topEdege) + CGFloat.init(truncating: bottomEdge))
        }else{
            return self.bounds
        }
    }
    
    open override func point(inside point: CGPoint, with event: UIEvent?) -> Bool {
        let buttonRect = self.hk_expandRect()
        if buttonRect.equalTo(self.bounds){
            return super.point(inside: point, with: event)
        }
        return buttonRect.contains(point)
    }
}
